<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <com-a></com-a>
      <div>-----------------</div>
      <com-b></com-b>
    </div>

    <template id="tmp1">
      <div>
        <h1>comA's msg: {{msg}}</h1>
        <h2>data:{{data}}</h2>
        <button @click="send">click send</button>
      </div>
    </template>

    <template id="tmp2">
      <div>
        <h1>comB's msg: {{msg}}</h1>
        <h2>data:{{data}}</h2>
        <button @click="send">click send</button>
      </div>
    </template>

    <template id="tmp2"> </template>

    <script src="./vue.js"></script>
    <script>
      let eventBus = new Vue();

      const comA = {
        data() {
          return {
            msg: "comA's message",
            data: "",
          };
        },
        methods: {
          send() {
            eventBus.$emit("AtoB", this.msg);
          },
        },
        mounted() {
          eventBus.$on("BtoA", (res) => {
            console.log(res);
            this.data = res;
          });
        },
        template: "#tmp1",
      };

      const comB = {
        data() {
          return {
            msg: "comB's message",
            data: "",
          };
        },
        methods: {
          send() {
            eventBus.$emit("BtoA", this.msg);
          },
        },
        mounted() {
          eventBus.$on("AtoB", (res) => {
            console.log(res);
            this.data = res;
          });
        },
        template: "#tmp2",
      };

      var vm = new Vue({
        el: "#app",
        data: {},
        methods: {},
        components: {
          comA,
          comB,
        },
      });
    </script>
  </body>
</html>
